<?php
class Bap_notifikasi_model extends MY_Model {
	
    function Bap_notifikasi_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "bap_notifikasi";
        $this->table_fields = array('bap_notif_id','bap_notif_target_user','bap_notif_type','bap_notif_post',
        	'bap_notif_look','bap_id');
    }
    
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
    }
        
    function select_by_id($id){
    	return $query = $this->db->get_where($this->table_name, array('bap_id' => $id));
    }
    
	function getNotification($targetUser, $type, $bap_id){
    	$nn = $this->get_where(array(
    		'bap_notif_target_user'=>$targetUser,
    		'bap_notif_type'=>$type,
    		'bap_id'=>$bap_id
    	));
    	$nn = $nn->row();
    	return (array)$nn;
    }
    
    function getNotification_by_id($bap_notif_id){
    	$nn = $this->get_where(array('bap_notif_id'=>$bap_notif_id));
    	$nn = $nn->row();
    	return (array)$nn;
    }
    
    function hasBeenOpened($targetUser, $type, $bap_id){
    	$notif = $this->getNotification($targetUser, $type, $bap_id);
    	
    	//tidak ada datanya, asmusikan sudah dibuka
    	if (!$notif) return true;
    	
    	$notif = (array)$notif;
    	if ($notif['bap_notif_look'] && $notif['bap_notif_look']!='0000-00-00 00:00:00') return true;
    	return false;
    }
    
    function updateOpened($perb_notif_id){
    	return $this->update(array('bap_notif_id'=>$perb_notif_id),array('bap_notif_look'=>date("Y-m-d H:i:s",time())));
    }
}